package com.superbytecode.cloud.safety.ratelimit.limiter;

import com.superbytecode.cloud.common.utils.http.HttpRequestUtil;
import com.superbytecode.cloud.safety.ratelimit.RateLimiter;
import com.superbytecode.cloud.safety.ratelimit.exception.RateLimitException;
import com.superbytecode.cloud.safety.ratelimit.model.RateLimitReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: beacon-cloud
 * @Package: com.superbytecode.cloud.safety.ratelimit.limiter
 * @ClassName: UserRateLimiter
 * @Description: [用户限流器]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/11/13 15:14
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Slf4j
public class UserRateLimiter extends RateLimiter {
    private static final String AUTHORIZATION_HEADER = "authorization";

    /**
     * 获取key
     *
     * @param rateLimitReq 请求参数
     * @return key
     */
    @Override
    protected String obtainKey(RateLimitReq rateLimitReq) {
        String authorization = HttpRequestUtil.obtainReqHeader(AUTHORIZATION_HEADER);
        if (StringUtils.isBlank(authorization)) {
            throw new RateLimitException("未登录用户不能使用用户限流");
        }
        return authorization;
    }
}